<template>
  <div class="right-toolbar">
    <el-row>
      <el-tooltip :show-after="500" class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
        <el-button :class="{'is-active': !showSearch}" size="small" circle :icon="Search" @click="toggleSearch()" />
      </el-tooltip>
      <el-tooltip :show-after="500" class="item" effect="dark" content="刷新" placement="top">
        <el-button size="small" circle :icon="Refresh" @click="refresh()" />
      </el-tooltip>
      <el-tooltip :show-after="500" v-if="columns.length > 0" class="item" effect="dark" content="显隐列" placement="top">
        <el-button size="small" circle :icon="Menu" @click="showColumn()" />
      </el-tooltip>
      <el-tooltip :show-after="500" v-if="showBack" class="item" effect="dark" content="返回上一页" placement="top">
        <el-button size="small" circle :icon="Back" @click="toBack" />
      </el-tooltip>
    </el-row>
    <el-dialog
      modal-class="not_center"
      :title="title"
      v-model="open"
      top="15vh"
      width="35vw"
      append-to-body
    >
      <el-transfer
        v-model="value"
        class="flex-center"
        :titles="['显示', '隐藏']"
        :data="columns"
        @change="dataChange"
      />
    </el-dialog>
  </div>
</template>

<script setup>
import { Back, Menu, Refresh, Search } from '@element-plus/icons-vue'

defineOptions({
  name: 'RightToolbar'
})

const router = useRouter()

const props = defineProps({
  showSearch: {
    type: Boolean,
    default: true
  },
  columns: {
    type: Array,
    default: () => []
  },
  showBack: {
    type: Boolean,
    default: false
  },
  backUrl: {
    type: String,
    default: ''
  }
})

const emits = defineEmits(['update:showSearch', 'queryTable', 'disable-columns'])

// 显隐数据
const value = ref([])
// 弹出层标题
const title = ref('显示/隐藏')
// 是否显示弹出层
const open = ref(false)

// 搜索
const toggleSearch = () => {
  emits('update:showSearch', !props.showSearch)
}

// 刷新
const refresh = () => {
  emits('queryTable')
}

// 右侧列表元素变化
const dataChange = (data) => {
  emits('disable-columns', data)
}

// 打开显隐列dialog
const showColumn = () => {
  open.value = true
}

// 返回上一页
const toBack = () => {
  props.backUrl ? router.push({path: props.backUrl}) : router.back()
}

// 初始化显隐列
onMounted(() => {
  // 显隐列初始默认隐藏列
  for (const item in props.columns) {
    if (props.columns[item].visible === false) {
      value.value.push(parseInt(item))
    }
  }
})
</script>

<style lang="scss" scoped>
.el-row + .right-toolbar {
  margin-left: 12px;
}

:deep(.el-button--small.is-circle) {
  width: 2rem;
}

.is-active {
  color: #409eff;
  border-color: rgb(197.7, 225.9, 255);
  background-color: rgb(235.9, 245.3, 255);
  outline: none;
}

:deep(.el-transfer__button) {
  border-radius: 50%;
  height: 32px;
  width: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 0;
}

:deep(.el-transfer__button:first-child) {
  margin-bottom: 10px;
}
</style>
